package luweiteng.demoForLogin.shiro.realm;

import luweiteng.demoForLogin.entity.TestUser;
import luweiteng.demoForLogin.service.TestUserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ConsumerRealm extends AuthorizingRealm {


    @Autowired
    private TestUserService userService ;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//        身份认证

        UsernamePasswordToken userToken = (UsernamePasswordToken) token;

        String userName = (String) userToken.getPrincipal();// 获取 用户名

        // 根据 userName 进行数据库的查询
        TestUser userInfo =  userService.getUserInfoByUserName(userName);

//      what the  means  of principal ？
//        what the means of credentials ?
//        credentials —— 凭证， 账号和密码
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(userInfo, userInfo.getPassword().getBytes(), getName());
        return simpleAuthenticationInfo;
    }
}
